package cn.javasea.atomikos.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = Ds1DataSourceConfiguration.PACKAGE, sqlSessionFactoryRef = "ds1SqlSessionFactory")
public class Ds1DataSourceConfiguration {
	static final String PACKAGE = "cn.javasea.atomikos.user.ds1";

	@Bean(name = "ds1")
	@ConfigurationProperties(prefix = "spring.datasource.ds1")
	@Primary
	public DataSource masterDataSource() {
		return new AtomikosDataSourceBean();
	}

	@Bean(name = "ds1SqlSessionFactory")
	@Primary
	public SqlSessionFactory ds1SqlSessionFactory() throws Exception {
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(masterDataSource());
		return sessionFactory.getObject();
	}
}
